package Com.itheima;

import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapDemo {
    public static void main(String[] args) {

        test1();
        System.out.println("------------------");
        test2();
    }

    public static void test1() {
        TreeMap<Student, String> treemap = new TreeMap<Student, String>();
        treemap.put(new Student("张三", 18), "北京");
        treemap.put(new Student("李四", 19), "上海");
        treemap.put(new Student("王五", 20), "广州");
        treemap.put(new Student("赵六", 21), "深圳");
        treemap.forEach((k,v)->{
            System.out.println(k + "-->" + v);

        });
    }
    public static void test2() {
        TreeMap<Student, String> treemap = new TreeMap<Student, String>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge() - o1.getAge();
            }
        });

        treemap.put(new Student("张三", 18), "北京");
        treemap.put(new Student("李四", 26), "上海");
        treemap.put(new Student("王五", 20), "广州");
        treemap.put(new Student("赵六", 16), "四川");

        treemap.forEach((k, v) -> System.out.println(k + "-->" + v));
    }


}
